<?php
/**
 * @title 150. 逆波兰表达式求值
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[] $tokens
     * @return Integer
     */
    function evalRPN($tokens) {
        /**
         * @since 2023-12-22 操作数的栈
         */
        $stack = new SplStack();

        /**
         * @since 2023-12-22 遍历字符串数组
         */
        foreach ($tokens as $token) {
            switch ($token){
                case "+":
                case "-":
                case "*":
                case "/":
                    /**
                     * @since 2023-12-22 弹出栈顶的两个元素，执行相应的操作，然后将结果压入栈中
                     * @since 2023-12-22 $num1 [+,-,*,/] $num2
                     */
                    $num2 = $stack->pop();
                    $num1 = $stack->pop();

                    switch ($token){
                        case "+":
                            $stack->push($num1 + $num2);
                            break;
                        case "-":
                            $stack->push($num1 - $num2);
                            break;
                        case "*":
                            $stack->push($num1 * $num2);
                            break;
                        case "/":
                            /**
                             * @since 2023-12-22 两个整数之间的除法总是向零截断
                             */
                            $stack->push(intval($num1 / $num2));
                            break;
                    }
                    // echo "num1={$num1}, num2={$num2}, token={$token}, result=", $stack->top(), PHP_EOL;
                    break;
                default:
                    /**
                     * @since 2023-12-22 操作数压入栈
                     */
                    $stack->push($token);
                    break;
            }
        }

        /**
         * @return
         */
        return $stack->pop();
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.22_9.php
 */
$datas = [
    [["4","-2","/","2","-3","-","-"]], // -7
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->evalRPN(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
